import { useEffect, useMemo, useState } from 'react';
import { Button, Spin, Tabs, message } from 'antd';
import { ReloadOutlined } from '@ant-design/icons';
// import { ContractHomeTabsPane } from './ContractHomeTabsPane';
import Cls from 'classnames';
import { ContractHomesAPI } from '../common/type';

import '../common/styles.less';
import pageStyles from '@/common/pageLayout.less';
import { contractHomeService, handlCheckManageService } from '../services';
import { ContractHomeCheckPane } from './ContractHomeCheckPane';
export const ContractHomeView: React.FC<ContractHomesAPI.IContractHomeView> = ({ curRes }) => {
  /** 样式根目录 */
  const styleFixCls = 'contract-homesty';

  /** 保存合同数据 */
  const [manageDataList, setManageDataList] = useState<Record<string, any>[]>([]);

  /** 保存的分页参数 */
  const [scrollObj, setScrollObj] = useState<any>({
    total: 0,
    count: 0,
    loading: false,
    hasMore: true,
    limit: 10,
  });

  /** 回调 */
  const homeCallback = (res: any) => {
    if (res && res?.msgFlag) {
      setManageDataList(res?.rows);
      setScrollObj({
        ...scrollObj,
        loading: false,
        total: res.total,
      });
    } else {
      message.error(res?.msg);
    }
  };

  /** 获取数据 */
  const { loading, reloadRefresh, getContractData } = contractHomeService(curRes, homeCallback);

  /** 获取代办已办数据 */
  const { taskLoading, getTaskCheckData, reloadTaskData } = handlCheckManageService(curRes, homeCallback);

  /** 存入选中的tab标识 */
  const [tabPaneKey, setTabPaneKey] = useState<string>('1');

  /** 初始化获取数据 */
  const defaultTaskCheckData = (switchBool: boolean) => {
    let { count, limit } = scrollObj;
    // count++;
    setScrollObj({
      ...scrollObj,
      count: count,
      loading: true,
    });
    let payload = {
      page: 1,
      rows: switchBool ? limit : 10 + limit,
    };
    getTaskCheckData({
      ...payload,
      taskType: tabPaneKey == '1' ? false : true,
    });
  };

  /** 返回子组件 */
  const renderTabsPane = () => {
    if (tabPaneKey == '1' || tabPaneKey == '4') {
      return (
        <ContractHomeCheckPane
          styleClassName={styleFixCls}
          contractInfoList={manageDataList}
          scrollObj={scrollObj}
          setScrollObj={setScrollObj}
          defaultTaskCheckData={defaultTaskCheckData}
        />
      );
    } else {
      // return <ContractHomeTabsPane styleClassName={styleFixCls} contractInfoList={manageDataList} />;
      return <></>;
    }
  };

  /** 刷新回调 */
  const reloadHomeCallback = () => {
    if (tabPaneKey == '1' || tabPaneKey == '4') {
      reloadTaskData();
    } else {
      reloadRefresh();
    }
  };

  /** 刷新 */
  const tabsExtraBtns = () => {
    return <Button type="primary" icon={<ReloadOutlined />} onClick={reloadHomeCallback} />;
  };

  /** tabs子页面 */
  const tabsItem = useMemo(() => {
    let items: any[] = [
      {
        label: '待办合同',
        key: '1',
        children: renderTabsPane(),
        isShow: curRes['/contractController/dealHomePage'] || false,
      },
      {
        label: '已办合同',
        key: '4',
        children: renderTabsPane(),
        isShow: curRes['/contractController/dealedHomePage'] || false,
      },
      // {
      //   label: '我的退回合同',
      //   key: '2',
      //   children: renderTabsPane(),
      //   isShow: curRes['/contractController/returnHomePage'] || false,
      // },
      // {
      //   label: '我的起草合同',
      //   key: '3',
      //   children: renderTabsPane(),
      //   isShow: curRes['/contractController/draftHomePage'] || false,
      // },
    ];

    const newItems: any[] = items?.filter((item: any) => item?.isShow);
    return newItems;
  }, [manageDataList, tabPaneKey, curRes, scrollObj]);

  useEffect(() => {
    // if (tabPaneKey == '1') {
    //   getTaskCheckData({
    //     taskType: false,
    //   });
    // } else if (tabPaneKey == '2') {
    //   getContractData({
    //     // state: `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1003`
    //     checkState: `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1001`,
    //   });
    // } else if (tabPaneKey == '3') {
    //   getContractData({
    //     state: `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1000`,
    //   });
    // } else if (tabPaneKey == '4') {
    //   getTaskCheckData({
    //     taskType: true,
    //   });
    // }
    if (tabPaneKey == '1' || tabPaneKey == '4') {
      defaultTaskCheckData(true);
    }
  }, [tabPaneKey]);

  return (
    <div className={Cls(`${styleFixCls}`, pageStyles['commonTabsLine'])}>
      <Spin spinning={loading || taskLoading}>
        <Tabs
          activeKey={tabPaneKey}
          tabBarExtraContent={tabsExtraBtns()}
          items={tabsItem}
          onChange={(activeKey: string) => {
            setScrollObj({
              total: 0,
              count: 0,
              loading: false,
              hasMore: true,
              limit: 10,
            });
            setManageDataList([]);
            setTabPaneKey(activeKey);
          }}
        />
      </Spin>
    </div>
  );
};
